package com.example.lxx.stickyexpandview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.lxx.stickyexpandview.model.EsalerAllCustomerModel;
import com.example.lxx.stickyexpandview.model.EsalerPermissionCustomersModel;
import com.example.lxx.stickyexpandview.model.EsalerPermissionTittleModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private List<MultiItemEntity> allList;
    private CustomersAdapter mAdapter;
    private RelativeLayout rlHeadView;
    private int mFirstVisiblePos;
    ViewGroup.MarginLayoutParams headerLayoutParams;
    private TextView mTittleView;
    private int mTop;
    private ImageView mArrowView;

    private EsalerAllCustomerModel tittleModel;
    private int index;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initRecycler();
        initData();
    }

    private void initData() {

        EsalerAllCustomerModel model0 = new EsalerAllCustomerModel();
        model0.setName("从分组中选择");
        model0.setId(0);
        for (int i = 0; i < 6; i++) {
            EsalerPermissionTittleModel model = new EsalerPermissionTittleModel();
            model.setType_id(0);

            model.setGroup_id("组id" + i);
            model.setGroup_name("组名称" + i);
            model.setAmout("组数量" + i);

            for (int j = 0; j < 8; j++) {
                EsalerPermissionCustomersModel mod = new EsalerPermissionCustomersModel();
                mod.setType_id(0);
                Random r = new Random();
                int k = r.nextInt(3);

                mod.setId("组客户id " + j);
                mod.setName("组客户name " + j);
                mod.setHead_portrait("" + k);
                mod.setMobile("110" + j);
                mod.setProvince("组客户name " + j);
                mod.setCity("???" + j);
                mod.setAddress("组客户name " + j);
                mod.setIs_permission_customer(0);
                mod.setIs_notfull_type(k);
                mod.setIs_notfull_name("信息合法 " + j);

                model.addSubItem(mod);
            }
            model0.addSubItem(model);
        }
        allList.add(model0);

        /**
         * 客户列表
         */
        EsalerAllCustomerModel model1 = new EsalerAllCustomerModel();
        model1.setName("从列表中选择");
        for (int j = 0; j < 20; j++) {
            EsalerPermissionCustomersModel mod = new EsalerPermissionCustomersModel();
            mod.setType_id(1);
            Random r = new Random();
            int i = r.nextInt(3);

            mod.setId("列表name " + j);
            mod.setName("列表name " + j);
            mod.setHead_portrait(i + "");
            mod.setMobile("10086");
            mod.setProvince("列表地址 " + j);
            mod.setCity("地址 " + j);
            mod.setAddress("地址 " + j);
            mod.setIs_permission_customer(0);
            mod.setIs_notfull_type(i);
            mod.setIs_notfull_name("???" + j);

            model1.addSubItem(mod);
        }

        allList.add(model1);
        Log.d("gggg", "数据集合长度: " + allList.size());

        mAdapter.notifyDataSetChanged();

        mAdapter.expand(0);
        mAdapter.expand(7);
    }

    private void initView() {
        allList = new ArrayList<>();
        mRecyclerView = findViewById(R.id.recycler_view_ll);

        rlHeadView = findViewById(R.id.rl_head_view);
        mTittleView = findViewById(R.id.tv_first_tittle_name);
        mArrowView = findViewById(R.id.iv_first_arrow);
        headerLayoutParams = (ViewGroup.MarginLayoutParams) rlHeadView.getLayoutParams();

        mArrowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mmp();
            }
        });
    }

    private void mmp() {
        if (tittleModel.isExpanded()) {
            mAdapter.collapse(index, false);
        } else {
            mAdapter.expand(index, false);
        }

        if (tittleModel.isExpanded()) {
            mArrowView.setImageResource(R.mipmap.esaler_up_arrow);
        } else {
            mArrowView.setImageResource(R.mipmap.esaler_down_arrow);
        }

        if (tittleModel.getId() == 0){
            mRecyclerView.scrollToPosition(0);
        }

    }

    private void initRecycler() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CustomersAdapter(allList);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnClickFirstExpandListener(new CustomersAdapter.OnClickFirstExpandListener() {
            @Override
            public void expandFirst(int position, ImageView imageView, EsalerAllCustomerModel model) {
                if (model.isExpanded()) {
                    mAdapter.collapse(position, true);
                } else {
                    mAdapter.expand(position, true);
                }
                mAdapter.notifyDataSetChanged();
            }
        });

        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager,只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法

                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                    //获取第一个可见view的位置
                    mFirstVisiblePos = linearManager.findFirstVisibleItemPosition();

                    /*----------------------设置浮动条的点击事件和图片状态-------------------------------------------------------------*/
                    if (mFirstVisiblePos == 0) {
                        tittleModel = (EsalerAllCustomerModel) allList.get(mFirstVisiblePos);
                        index = 0;
                        mTittleView.setText("从分组中选择");
                    }

                    if (mFirstVisiblePos != 0 && (allList.get(mFirstVisiblePos) instanceof EsalerAllCustomerModel)) {
                        if (allList.get(mFirstVisiblePos + 1) instanceof EsalerPermissionCustomersModel) {
                            tittleModel = (EsalerAllCustomerModel) allList.get(mFirstVisiblePos);
                            index = mFirstVisiblePos;
                        } else {
                            tittleModel = (EsalerAllCustomerModel) allList.get(0);
                            index = 0;
                        }
                    }


                    if (tittleModel.isExpanded()) {
                        mArrowView.setImageResource(R.mipmap.esaler_up_arrow);
                    } else {
                        mArrowView.setImageResource(R.mipmap.esaler_down_arrow);
                    }
                    /*------------------------------设置浮动条的点击事件和图片状态-------------------------------------------------------*/


                    if (allList.get(mFirstVisiblePos + 1) instanceof EsalerAllCustomerModel) {

                        tittleModel = (EsalerAllCustomerModel) allList.get(0);
                        index = 0;
                        mTittleView.setText("从分组中选择5");



                        EsalerAllCustomerModel model = (EsalerAllCustomerModel) allList.get(mFirstVisiblePos + 1);
                        View view = linearManager.findViewByPosition(mFirstVisiblePos + 1);
                        int height = view.getHeight();
                        mTop = view.getTop();
                        Log.d("gggg", "第一个可见view: " + model.getItemType() + "/位置" + mFirstVisiblePos + "/高度 " + height + "/父布局顶部距离 " + mTop);
                        Log.d("gggg", "第一个可见view的下一个view: " + model.getItemType() + "/位置" + (mFirstVisiblePos + 1) + "/高度 " + height + "/父布局顶部距离 " + mTop + "/上滑？下滑" + dy);


                        if (mTop <= 90) {
                            headerLayoutParams.topMargin = -(90 - mTop);
                            rlHeadView.setLayoutParams(headerLayoutParams);
                        }

                        if (mTop <= 0) {
                            headerLayoutParams.topMargin = 0;
                            rlHeadView.setLayoutParams(headerLayoutParams);

                            mTittleView.setText("从列表中选择1");
                        } else if (mTop > 80 && ((allList.get(mFirstVisiblePos) instanceof EsalerAllCustomerModel) && (allList.get(mFirstVisiblePos + 1) instanceof EsalerAllCustomerModel))) {
                            headerLayoutParams.topMargin = 0;
                            rlHeadView.setLayoutParams(headerLayoutParams);
                            mTittleView.setText("从分组中选择1");

                            tittleModel = (EsalerAllCustomerModel) allList.get(mFirstVisiblePos);
                            index = 0;
                        }

                    } else {

                        if (mFirstVisiblePos == 0) {
                            headerLayoutParams.topMargin = 0;
                            rlHeadView.setLayoutParams(headerLayoutParams);
                            mTittleView.setText("从分组中选择2");

                            tittleModel = (EsalerAllCustomerModel) allList.get(mFirstVisiblePos);
                            index = 0;

                        }
                        if (tittleModel.getId() == 0) {
                            headerLayoutParams.topMargin = 0;
                            rlHeadView.setLayoutParams(headerLayoutParams);

                            mTittleView.setText("从分组中选择3");
                        } else {
                            headerLayoutParams.topMargin = 0;
                            rlHeadView.setLayoutParams(headerLayoutParams);
                            mTittleView.setText("从列表中选择2");
                        }

                    }

                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);


            }
        });


        /**
         * 二级列表展开事件(组客户)
         */
        mAdapter.setOnClickSecondExpandListener(new CustomersAdapter.OnClickSecondExpandListener() {
            @Override
            public void expandSecond(int position, ImageView ivCheck, ImageView ivArrow, EsalerPermissionTittleModel model) {
                if (model.isExpanded()) {
                    mAdapter.collapse(position, false);
                } else {
                    mAdapter.expand(position, false);
                }
            }
        });

        /**
         * 三级列表(/列表客户)选择事件
         */
        mAdapter.setOnChildItemClickListener(new CustomersAdapter.OnChildItemClickListener() {
            @Override
            public void childItemClick(int position, EsalerPermissionCustomersModel model, boolean isCheck) {
                if (0 == model.getIs_notfull_type()) {
                    model.setIsSelected(!isCheck);
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void setTopMargin(LinearLayoutManager linearManager, int index) {

    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public int px2dip(Context context, int pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
